package com.shundr.shipper.common.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.shundr.shipper.R;
import com.shundr.shipper.common.model.FavoriteInfo;
import com.shundr.shipper.common.util.CommonConfig;
import com.shundr.shipper.common.util.DateUtil;

public class FavoriteAdapter extends BaseAdapter {
	public List<FavoriteInfo> mList;
	private Context mContext;
	
	private int[] res= {R.drawable.circle_text_call,R.drawable.circle_text_detail,R.drawable.circle_text_fav};
	
	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int index) {
		return mList.get(index);
	}

	@Override
	public long getItemId(int id) {
		return id;
	}

	public FavoriteAdapter(Context mContext,List<FavoriteInfo> list) {
		super();
		this.mList = list;
		this.mContext = mContext;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup viewGroup) {
		ViewHold viewHold = null;
		if(convertView == null){
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_favorite,null);
			viewHold = new ViewHold();
			viewHold.tvObjType = (TextView) convertView.findViewById(R.id.tv_object_type);
			viewHold.tvTime = (TextView) convertView.findViewById(R.id.tv_time);
			viewHold.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
			viewHold.tvOpType = (TextView) convertView.findViewById(R.id.tv_op_type);
			convertView.setTag(viewHold);
		}else{
			viewHold = (ViewHold) convertView.getTag();
		}
		try {
			FavoriteInfo info = mList.get(position);
			switch (info.getObjectType()) {
			case CommonConfig.OPERATE_OBJECT_CARGO:
				viewHold.tvObjType.setText("货源");
				viewHold.tvObjType.setBackgroundResource(res[0]);
				break;
			case CommonConfig.OPERATE_OBJECT_TRUCK:
				viewHold.tvObjType.setText("车源");
				viewHold.tvObjType.setBackgroundResource(res[1]);
				break;
			case CommonConfig.OPERATE_OBJECT_OWNER:
				viewHold.tvObjType.setText("货主");
				viewHold.tvObjType.setBackgroundResource(res[2]);
				break;
			case CommonConfig.OPERATE_OBJECT_TRUCKER:
				viewHold.tvObjType.setText("车主");
				viewHold.tvObjType.setBackgroundResource(res[3]);
				break;
			default:
				break;
			}
			
//			int rand = new Random().nextInt(3);
//			viewHold.tvObjType.setBackgroundResource(res[rand]);
			viewHold.tvTime.setText(DateUtil.getInterval(info.getInsertTime()));
			viewHold.tvTitle.setText(info.getFavoriteTitle().replace("null", ""));
//			switch (info.getObjectType()) {
//			case CommonConfig.OPERATE_WATCH:
//				viewHold.tvOpType.setText("查看详情");
//				break;
//			case CommonConfig.OPERATE_CALL:
//				viewHold.tvOpType.setText("打电话");
//				break;
//
//			default:
//				break;
//			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}

	static class ViewHold{
		TextView tvObjType;
		TextView tvTime;
		TextView tvTitle;
		TextView tvOpType;
	}
}
